package com.jfinal.code.eclipse.plugin.wizards.page;

import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.wizard.WizardPage;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Text;

import com.jfinal.code.eclipse.plugin.generator.db.DbType;
import com.jfinal.code.eclipse.plugin.generator.dto.DatabaseInfoValueDTO;
import com.jfinal.code.eclipse.plugin.i18n.I18n;
import com.jfinal.code.eclipse.plugin.wizards.ui.DatabaseInfoUI;

/**
 * 数据库连接信息引导页面
 * <p>
 * 年华似水匆匆一瞥，多少岁月轻描淡写。
 * </p>
 * @author yaolin
 * @see org.eclipse.jface.wizard.WizardPage
 */
public class DatabaseInfoWizardPage extends WizardPage {

	private static final String PAGE_NAME = "DatabaseInfoWizardPage";
	private ISelection selection;

	// UI Element
	private Button loadPropertyButton; // 加载配置文件
	private Text propertyText; // 配置文件
	private Combo databaseTypeCombo; // 数据库类型
	private Text serverNameText; // 数据库服务名称(IP)
	private Text portText; // 端口
	private Text databaseNameText; // 数据库名称
	private Text usernameText; // 用户名
	private Text passwordText; // 密码
	//private Button useDefaultDriverButton; // 是否使用默认的驱动
	private Text driverLocationText; // 驱动包的位置
	private Text jdbcUrlText; // jdbcUrl
	private Text jdbcDriverClassNameText; // jdbcClassName

	public DatabaseInfoWizardPage(ISelection selection) {
		super(PAGE_NAME);
		this.selection = selection;
		setTitle(I18n.databaseInfoWp_title);
		setDescription(I18n.databaseInfoWp_description);
	}

	@Override
	public void createControl(Composite parent) {
		// adjust parent
		Composite container = new Composite(parent, SWT.NULL);
		GridLayout layout = new GridLayout(3, false);
		container.setLayout(layout);
		layout.verticalSpacing = 5;
		layout.horizontalSpacing = 5;
		
		// 创建一系列控件
		DatabaseInfoUI ui = new DatabaseInfoUI(this, getShell(), container);
		loadPropertyButton = ui.createLoadPropertyButton();
		propertyText = ui.createPropertyText();
		databaseTypeCombo = ui.createDatabaseTypeCombo();
		serverNameText = ui.createServerNameText();
		portText = ui.createPortText();
		databaseNameText = ui.createDatabaseNameText();
		usernameText = ui.createUsernameText();
		passwordText = ui.createPasswordText();
		driverLocationText = ui.createDriverLocationText();
		jdbcUrlText = ui.createJdbcUrlText();
		jdbcDriverClassNameText = ui.createJdbcDriverClassNameText();
		setDefaultValue();
		setControl(container);
	}
	
	/**
	 * 重写setVisible进行数据校验
	 */
	@Override
	public void setVisible(boolean visible) {
		// validate 这里在createControl进行校验
		DatabaseInfoUI.Validator.validatorEmptyString(this);
		super.setVisible(visible);
	}

	/**
	 * 设置控件的默认值
	 */
	private void setDefaultValue() {
		serverNameText.setText("localhost");
		portText.setText("3306");
		databaseNameText.setText("test");
		usernameText.setText("root");
		passwordText.setText("root");
	}
	
	/**
	 * 获取数据库填写的信息
	 */
	public DatabaseInfoValueDTO getValue() {
		DatabaseInfoValueDTO dto = new DatabaseInfoValueDTO();
		dto.setDbType(DbType.getByOrdinal(databaseTypeCombo.getSelectionIndex()));
		dto.setJarFile(driverLocationText.getText().trim());
		dto.setUrl(jdbcUrlText.getText().trim());
		dto.setDriverClassName(jdbcDriverClassNameText.getText().trim());
		dto.setUsername(usernameText.getText().trim());
		dto.setPassword(passwordText.getText().trim());
		return dto;
	}
	// -----------getter---------------
	public ISelection getSelection() {
		return selection;
	}
	public Button getLoadPropertyButton() {
		return loadPropertyButton;
	}
	public Text getPropertyText() {
		return propertyText;
	}
	public Combo getDatabaseTypeCombo() {
		return databaseTypeCombo;
	}
	public Text getServerNameText() {
		return serverNameText;
	}
	public Text getPortText() {
		return portText;
	}
	public Text getDatabaseNameText() {
		return databaseNameText;
	}
	public Text getUsernameText() {
		return usernameText;
	}
	public Text getPasswordText() {
		return passwordText;
	}
	public Text getDriverLocationText() {
		return driverLocationText;
	}
	public Text getJdbcUrlText() {
		return jdbcUrlText;
	}
	public Text getJdbcDriverClassNameText() {
		return jdbcDriverClassNameText;
	}
}
